package com.caocaog.market.service.impl;


import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.caocaog.market.entity.GoodsAttr;
import com.caocaog.market.mapper.GoodsAttrMapper;
import com.caocaog.market.service.GoodsAttrService;
import com.caocaog.market.vo.GoodsAttributeVo;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.constant.CacheConstant;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.system.base.entity.FieldRedisItem;
import org.jeecg.common.system.base.service.impl.GenericServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

/**
 * 商品属性
 *
 * @author chechen
 * @date 2022-04-12
 */
@Slf4j
@Service
public class GoodsAttrServiceImpl extends GenericServiceImpl<GoodsAttrMapper, GoodsAttr> implements GoodsAttrService {


    @Override
    public List<GoodsAttributeVo> getGoodsAttrVOByGoodsId(String goodsId) {
        List<GoodsAttr> list = selectListByFieldCache(goodsId, CacheConstant.GOODS_ATTR_LIST_BY_GOODS_ID);
        List<GoodsAttributeVo> voList = new ArrayList<>();
        for (GoodsAttr attribute : list) {
            GoodsAttributeVo vo = new GoodsAttributeVo();
            vo.setId(attribute.getId());
            vo.setKey(attribute.getId());
            vo.setEditable(false);
            vo.setNameSku(attribute.getName());
            String[] values = attribute.getValue().split(StringPool.COMMA);
            vo.setValueSku(values);
            voList.add(vo);
        }
        return voList;
    }


    @Override
    public GoodsAttr getGoodsAttr(String id) {
        return getByIdCache(id);
    }


    @Override
    public List<GoodsAttr> getGoodsAttrByGoodsId(String goodsId) {
        return selectListByFieldCache(goodsId, CacheConstant.GOODS_ATTR_LIST_BY_GOODS_ID);
    }


    @Override
    public GoodsAttr insertGoodsAttr(GoodsAttr goodsAttr) {
        insertCache(goodsAttr);
        return getByIdCache(goodsAttr.getId());
    }


    @Override
    public GoodsAttr updateGoodsAttr(GoodsAttr goodsAttr) {
        updateCache(goodsAttr);
        return getByIdCache(goodsAttr.getId());
    }


    @Override
    public int insertBatchGoodsAttr(List<GoodsAttr> goodsAttrs) {
        int insertBatchRecordCount = insertBatchCache(goodsAttrs);
        if (insertBatchRecordCount == 0) {
            goodsAttrs.forEach(goodsAttrsError -> log.error(goodsAttrsError.toString()));
            throw new JeecgBootException("插入商品属性失败, list-^");
        } else if (insertBatchRecordCount < goodsAttrs.size()) {
            goodsAttrs.forEach(goodsAttrsError -> log.info(goodsAttrsError.toString()));
            throw new JeecgBootException("没有全部成功插入商品属性, list-^");
        }
        return insertBatchRecordCount;
    }


    @Override
    public int updateBatchGoodsAttr(List<GoodsAttr> goodsAttrs) {
        int updateBatchRecordCount = updateBatchCache(goodsAttrs);
        if (updateBatchRecordCount == 0) {
            goodsAttrs.forEach(goodsAttrsError -> log.error(goodsAttrsError.toString()));
            throw new JeecgBootException("更新商品属性失败, list-^");
        } else if (updateBatchRecordCount < goodsAttrs.size()) {
            goodsAttrs.forEach(goodsAttrsError -> log.info(goodsAttrsError.toString()));
            throw new JeecgBootException("没有全部成功更新商品属性, list-^");
        }
        return updateBatchRecordCount;
    }


    @Override
    public int deleteGoodsAttrByGoodsId(String goodsId) {
        return deleteByEntityListAllCache(selectListByFieldCache(goodsId, CacheConstant.GOODS_ATTR_LIST_BY_GOODS_ID));
    }


    @Override
    public String[] getGoodsAttrIdsByGoodsIds(String... goodsIds) {
        return Arrays.stream(goodsIds)
                .distinct()
                .map(goodsId -> this.selectListByFieldCache(goodsId, CacheConstant.GOODS_ATTR_LIST_BY_GOODS_ID))
                .flatMap(Collection::stream)
                .map(GoodsAttr::getId)
                .toArray(String[]::new);
    }


    @Override
    public String getName() {
        return "商品属性";
    }

    @Override
    public List<FieldRedisItem<GoodsAttr>> getAllFieldCacheList() {
        return Arrays.asList(
                FieldRedisItem.build(GoodsAttr::getId, CacheConstant.GOODS_ATTR_INFO, true),
                FieldRedisItem.build(GoodsAttr::getGoodsId, CacheConstant.GOODS_ATTR_LIST_BY_GOODS_ID)
        );
    }

}
